<template>
	<view class="constainer">
		<!-- 顶部搜索 -->
		<view class="top-search">
			<input class="top-input" v-model="keyword" placeholder="搜索精彩运动">
			<view class="aaaaaa" @click.stop="getlist">
				<img class="input-icon" src="/static/images/icon/search.png" alt="">
			</view></input>
		</view>
		<!-- 商品列表信息 -->
		<view class="nodata" v-if="JSON.stringify(data)=='[]'">
			没有当前搜索数据
		</view>
		<view class="scroll-view">
			<view class="commodity-item" v-for="(item,index) in data" :key="item.pid">
				<view class="item-top">
					<view class="item-top-img"><img :src="item.image" @error='errorimg(index)' alt="">
					</view>
					<view class="item-top-data">
						<view class="data-view">
							<view class="data-name">{{item.title}}</view>
							<button class="data-botton" @click="onPayment(item)">立即付款</button>
						</view>
						<view class="data-view">
							<view class="data-price">${{item.price}}

							</view>
							<button class="data-shop" @click="setShopCart(item.pid,item.amount)">加入购物车</button>
						</view>
					</view>
				</view>
				<view class="item-bottom">
					<view class="item-bottom-name">
						推荐度
					</view>
					<vant-progress :percentage="item.hot" pivot-text=' ' class="progress"></vant-progress>
					<uni-number-box v-model="item.amount" background='#fff' backgroundItem="#fff" class='number-box'>
					</uni-number-box>
				</view>
			</view>
		</view>

	</view>
</template>

<script>
	import {
		likeList,
		addCart,

	} from '@/api/system/shop.js'
	import {
		createOrder
	} from "@/api/system/order.js"
	export default {
		data() {
			return {
				data: '',
				keyword: ''
			};
		},
		methods: {
			onPayment(item) {
				if (item.amount == 0) return this.$modal.msg('请加入购买数量')
				const data = {
					pid: item.pid,
					amount: item.amount
				}
				createOrder(data).then(res => {
					if (res.code == 200) {
						this.$tab.navigateTo('/pages/car/payment?data=' + JSON.stringify(res.data))
					} else {
						this.$modal.msgError(res.msg)
					}
				})

			},
			getlist() {
				if (this.keyword.trim() == '') return this.$modal.msgError('请输入搜索内容')
				const data = {
					keyword: this.keyword
				}
				likeList(data).then(res => {
					if (res.code != 200) return this.$modal.msgError(res.msg)
					res.data.forEach(res => {
						res.amount = 0
						res.image ? '' : res.image = '/static/images/banner/banner01.jpg'
					})
					this.data = res.data
				})
			},
			errorimg(id) {
				return this.data[id].image = '/static/images/banner/banner01.jpg'
			},
			setShopCart(pid, amount) {
				if (amount == 0) return this.$modal.msg('请加入购买数量')
				const data = {
					pid,
					amount
				}
				addCart(data).then(res => {
					console.log(res)
				})
			},

		},

	}
</script>
<style>
	page {
		background-color: #65A8F2;
	}
</style>
<style lang="less" scoped>
	// 基础设置
	.constainer {
		margin: 0;
		padding: 0;
		height: 100vh;
		font-family: "Arial";
		position: relative;
	}

	.nodata {
		text-align: center;
		margin: 60rpx;
		font-weight: 600;
		font-size: 40rpx;
		color: #ccc;
	}

	// 顶部搜索框
	.top-search {
		display: flex;
		justify-content: center;
		margin-left: 5%;

		.top-input {
			width: 1300rpx;
			height: 80rpx;
			color: rgba(136, 136, 136, 1);
			padding-left: 30rpx;
			margin-top: 20rpx;
			border-radius: 40rpx;
			border: 1px solid rgba(187, 187, 187, 1);
		}

		.aaaaaa {
			position: relative;
			width: 100rpx;
			height: 80rpx;
			right: 90rpx;
			top: 20rpx;
			z-index: 999;

			.input-icon {
				margin-top: 20rpx;
				width: 48rpx;
				height: 48rpx;
			}
		}


	}


	.scroll-view {
		margin-top: 40rpx;
	}

	.commodity-item {
		margin: 0 auto 26rpx;
		height: 250rpx;
		width: 690rpx;
		border: 2rpx solid rgba(187, 187, 187, 1);
		background-color: #fff;

		.item-top {
			display: flex;

			.item-top-img {
				width: 238rpx;
				height: 174rpx;
				margin: 8rpx;

				img {
					width: 100%;
					height: 100%;
					border-radius: 50rpx;
				}
			}

			.data-view {
				display: flex;
				width: 436rpx;
				padding: 0 18rpx;
				margin: 10rpx 0 0 0;
				justify-content: space-between;

				.data-name {
					font-size: 30rpx;
					line-height: 60rpx;
					font-family: SourceHanSansSC-regular;
				}

				.data-botton {
					margin: 0;
					width: 180rpx;
					height: 60rpx;
					color: #fff;
					border-radius: 20rpx;
					background-color: rgba(83, 150, 247, 1);
					font-size: 28rpx;
					text-align: center;
					line-height: 60rpx;
					border: 2rpx solid rgba(187, 187, 187, 1);
				}

				.data-shop {
					margin: 0;
					padding: 0;
					width: 180rpx;
					height: 60rpx;
					line-height: 60rpx;
					margin-top: 10rpx;
					border-radius: 20rpx;
					background-color: rgba(255, 255, 255, 1);
					color: rgba(79, 141, 247, 1);
					font-size: 28rpx;
					text-align: center;
					border: 4rpx solid rgba(79, 141, 247, 1);
				}

				.data-price {
					font-size: 36rpx;
					font-weight: 700;
					margin-top: 40rpx;
				}
			}
		}
	}

	.item-bottom {
		display: flex;
		margin-right: 20rpx;

		.item-bottom-name {
			height: 18rpx;
			margin: 0 0 18rpx 40rpx;
			font-size: 12px;
		}

		.progress {
			width: 340rpx;
			margin: auto;
			position: relative;
			bottom: 6rpx;
		}
	}

	//组件样式

	::v-deep .uni-numbox-btns {
		border: 1px solid rgba(79, 141, 247, 1);
		width: 50rpx;

	}

	.number-box {
		position: relative;
		bottom: 10rpx;
	}
</style>
